package test.login.login2;



import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {
    private static String URL;
    private static String DRIVER;
    private static String USERNAME;
    private static String PASSWORD;
    private static Connection con;
    private static PreparedStatement ps=null;
    private static ResultSet re=null;
    static{
        try {
            Properties properties = new Properties ( );
            InputStream is = JdbcUtil.class.getClassLoader ( ).getResourceAsStream ("mysqlme.properties");
            properties.load (is);
            DRIVER=properties.getProperty ("driver");
            URL=properties.getProperty ("url");
            USERNAME=properties.getProperty ("username");
            PASSWORD=properties.getProperty ("password");
           Class.forName (DRIVER);
          con = DriverManager.getConnection (URL, USERNAME, PASSWORD);
        } catch (IOException e) {
            e.printStackTrace ( );
        } catch (SQLException e) {
            e.printStackTrace ( );
        } catch (ClassNotFoundException e) {
            e.printStackTrace ( );
        }
    }

    public static Connection getCon() {
        return con;
    }
    public static PreparedStatement getPs(String sql){
        try {
            ps=con.prepareStatement (sql);
        } catch (SQLException e) {
            e.printStackTrace ( );
        }
        return ps;
    }

    public static ResultSet getRe() {
        try {
           re= ps.executeQuery ();
        } catch (SQLException e) {
            e.printStackTrace ( );
        }
        return re;
    }
    int i=0;
    public  static int update(){
        int i= 0;
        try {
            i = ps.executeUpdate ( );
        } catch (SQLException e) {
            e.printStackTrace ( );
        }
        return i;
    }
    public  static void closeall(){
        if (re!=null){
            try {
                re.close ();
            } catch (SQLException e) {
                e.printStackTrace ( );
            }
        }
        if (ps!=null){
            try {
                ps.close ();
            } catch (SQLException e) {
                e.printStackTrace ( );
            }
        }
        if (con!=null){
            try {
                con.close ();
            } catch (SQLException e) {
                e.printStackTrace ( );
            }
        }
    }
}
